package top.tangyh.lamp.care.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.tangyh.basic.base.R;
import top.tangyh.basic.exception.BizException;
import top.tangyh.basic.interfaces.echo.EchoService;
import top.tangyh.basic.base.controller.SuperController;
import top.tangyh.lamp.care.service.BCarePgVteService;

import top.tangyh.lamp.care.entity.BCarePgVte;
import top.tangyh.lamp.care.vo.result.BCarePgVteVO;
import top.tangyh.lamp.care.vo.save.BCarePgVteSaveVO;
import top.tangyh.lamp.care.vo.save.PgRecordDetailVo;
import top.tangyh.lamp.care.vo.update.BCarePgVteUpdateVO;
import top.tangyh.lamp.care.vo.result.BCarePgVteResultVO;
import top.tangyh.lamp.care.vo.query.BCarePgVtePageQuery;
import top.tangyh.lamp.common.utils.PDateUtils;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import static top.tangyh.lamp.common.constant.SwaggerConstants.DATA_TYPE_LONG;
import static top.tangyh.lamp.common.constant.SwaggerConstants.DATA_TYPE_STRING;

/**
 * <p>
 * 前端控制器
 * vte风险评分
 * </p>
 *
 * @author zqb
 * @date 2024-07-08 16:40:03
 * @create [2024-07-08 16:40:03] [zqb] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/vte")
@Tag(name = "系统：VTE风险评分")
public class BCarePgVteController {
    //extends SuperController<BCarePgVteService, Integer, BCarePgVte
    //    , BCarePgVteSaveVO, BCarePgVteUpdateVO, BCarePgVtePageQuery, BCarePgVteResultVO>
//    private final EchoService echoService;
//    @Override
//    public EchoService getEchoService() {
//        return echoService;
//    }

    private final BCarePgVteService bCarePgVteService;


    @Operation(summary = "新增vte风险评分 （APACHEll评分也调用这个）\"")
    @PostMapping("/add")
    public R add(@RequestBody BCarePgVteSaveVO bCarePgVteSaveVO) {
        BCarePgVte po = BeanUtil.toBean(bCarePgVteSaveVO, BCarePgVte.class);
//        ConvertUtil.convert(yxCarePgVteVO, BCarePgVte.class);
        bCarePgVteService.checkPg(bCarePgVteSaveVO);
        bCarePgVteService.save(po);
        return R.success(po.getId());
    }

    @Operation(summary = "更新vte风险评分 （医生APACHEll评分也调用这个）")
    @PutMapping("/carePgRecord")
    public R updateById(@Valid @RequestBody BCarePgVteUpdateVO bCarePgVteUpdateVO) {
        BCarePgVte po = BeanUtil.toBean(bCarePgVteUpdateVO, BCarePgVte.class);
        if (null != po.getId()) {
            BCarePgVte old = bCarePgVteService.getById(po.getId());
//必须本人才能修改
//            if(null != SecurityUtil.checkRecordAuth(old.getCreateBy() ,old.getUpdateBy())){
//                throw new BadRequestException(SecurityUtil.checkRecordAuth(old.getCreateBy() ,old.getUpdateBy()));
//            }
        }
        po.setUpdatedTime(LocalDateTime.now());
        BCarePgVte result = bCarePgVteService.updateById(po);
        if (null != result.getId()) {
            return R.success();
        } else {
            return R.fail("更新失败");
        }
    }

    @GetMapping(value = "/list")
    @Operation(summary = "查询列表(护理记录模块需要调用)")
    public R<List<BCarePgVte>> statisticInAndOutSum(@RequestParam String patientIdent, @RequestParam Integer type,
                                                    @RequestParam(required = false) Long rkTime, @RequestParam(required = false) Long ckTime) {
        QueryWrapper<BCarePgVte> wrapper = new QueryWrapper<>();
        wrapper.eq("type", type);
//        wrapper.eq("is_deleted",0);
        wrapper.eq("patient_ident", patientIdent);
        if (null != rkTime) {
            wrapper.ge("pf_time",  PDateUtils.dateToStr(new Date(rkTime)));
        }
        if (null != ckTime) {
            wrapper.le("pf_time", PDateUtils.dateToStr(new Date(ckTime)));
        }
        wrapper.orderByDesc("pf_time");
        wrapper.eq("is_deleted", 0);
        List<BCarePgVte> list = bCarePgVteService.list(wrapper);
        return R.success(list);
    }


    @Operation(summary = "根据ID查询vte风险评分详情")
    @GetMapping(value = "detail/{id}")
    public R<BCarePgVte> getCarePgRecord(@PathVariable("id") Long id) {
        if (null != id) {
            return R.success(bCarePgVteService.getById(id ));
        } else {
            return R.success(new BCarePgVte());
        }
    }

    @Operation(summary = "查询最新的一条数据 根据页面传过来的时间")
    @GetMapping(value = "/last/new")
    @Parameters({
            @Parameter(name = "type", description = "类型  1 vte 2 肌酐 3 APACHEll", required = false),
            @Parameter(name = "date", description = "评分日期", required = false),
            @Parameter(name = "patientIdent", description = "病人识别号", required = false),
    })
    public R<BCarePgVte> getLastNewCarePgRecord(@RequestParam int type, @RequestParam(required = false) Long date,
                                                @RequestParam(required = false) String patientIdent) {
        String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date(date));
        return R.success(bCarePgVteService.getLastNewCarePgRecord(type, dateStr, patientIdent));
    }


    @Operation(summary = "查询第一次数据")
    @GetMapping(value = "/first/record")
    @Parameters({
            @Parameter(name = "type", description = "类型  1 vte 2 肌酐 3 APACHEll", required = false),
            @Parameter(name = "date", description = "评分日期", required = false),
            @Parameter(name = "patientIdent", description = "病人识别号", required = false),
    })
    public R<BCarePgVte> getFirstCarePgRecord(@RequestParam int type, @RequestParam(required = false) Long date,
                                              @RequestParam(required = false) String patientIdent) {
        String dateStr = "";
        if (null != date) {
            dateStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date(date));
        }
        return R.success(bCarePgVteService.getFirstCarePgRecord(type, dateStr, patientIdent));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除 医生APACHEll 评分")
    public R<Boolean> delete(@RequestParam Long id) {
        BCarePgVte carePgVtePO = new BCarePgVte();
        if (null != id) {
//            BCarePgVte old = yxCarePgVteService.getById(id);
//            if(null != SecurityUtil.checkRecordAuth(old.getCreateBy() ,old.getUpdateBy())){
//                throw new BizException(SecurityUtil.checkRecordAuth(old.getCreateBy() ,old.getUpdateBy()));
//            }
        }
        carePgVtePO.setId(id);
        carePgVtePO.setIsDeleted(1);
        bCarePgVteService.updateById(carePgVtePO);
        return R.success();
    }

    @Operation(summary = "质控查询某个病人最新评分里的三个总评分里的最新的评分")
    @GetMapping(value = "/score/one")
    public R<String> getVteOfTopNewScore(@RequestParam(required = true) String patientIdent) {
        return R.success(bCarePgVteService.getVteOfTopNewScore(patientIdent));
    }


}


